var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入swagger
var swaggerJSDoc = require('swagger-jsdoc');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var studentRouter = require('./routes/student/stundet');

var app = express();
// 定义swagger
var swaggerDefinition = {
  info: {
    title: '学生系统api',
    version: '1.1.0',
    description: '学生系统api测试',
    contact:{
    name: "张三",  //联系人
    email:"f77_code@163.com" , //联系人邮件
    url: "http://f77.com/support" //公司的网址
    }
  },
  host: 'localhost:9000',
  basePath: '/',
};

// swagger的文档的配置
var options = {
  // 引入swagger的定义
  swaggerDefinition: swaggerDefinition,
  //扫描api生成api的文档
  //** 表示所有的包  *.js 表示所有的js文件
  apis: ['./routes/**/*.js'],
};

//初始化swagger-jsdoc
var swaggerSpec = swaggerJSDoc(options);



// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/student', studentRouter);

// 读取swagger的信息
app.get('/swagger.json', function(req, res) {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
